<template>
  <a-layout-header class="header">
    <div class="header-container">
      <!-- 左侧：Logo和标题 -->
      <div class="header-left">
        <RouterLink to="/">
          <div class="logo-section">
            <img class="logo" src="@/assets/logo.png" alt="Logo" />
            <h1 class="site-title">智能AI应用生成</h1>
          </div>
        </RouterLink>
      </div>

      <!-- 中间：导航菜单 -->
      <div class="header-center">
        <a-menu
          v-model:selectedKeys="selectedKeys"
          mode="horizontal"
          :items="menuItems"
          @click="handleMenuClick"
          class="nav-menu"
        />
      </div>

      <!-- 右侧：用户操作区域 -->
      <div class="header-right">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown placement="bottomRight" :trigger="['hover']">
              <div class="user-avatar-wrapper">
                <a-space>
                  <a-avatar :src="loginUserStore.loginUser.userAvatar" />
                  <span>{{ loginUserStore.loginUser.userName ?? '未设置昵称' }}</span>
                </a-space>
              </div>
              <template #overlay>
                <a-menu>
                  <a-menu-item key="profile" @click="handleEditProfile">
                    <template #icon>
                      <UserOutlined />
                    </template>
                    编辑个人资料
                  </a-menu-item>
                  <a-menu-divider />
                  <a-menu-item key="logout" @click="handleLogout">
                    <template #icon>
                      <LogoutOutlined />
                    </template>
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" @click="handleLogin">登录</a-button>
          </div>
        </div>
      </div>
    </div>
  </a-layout-header>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { UserOutlined, LogoutOutlined } from '@ant-design/icons-vue'
import type { MenuProps } from 'ant-design-vue'
import { useLoginUserStore } from '@/stores/loginUser'

const loginUserStore = useLoginUserStore()

const router = useRouter()
// 当前选中菜单
const selectedKeys = ref<string[]>(['/'])
// 监听路由变化，更新当前选中菜单
router.afterEach((to, from, next) => {
  selectedKeys.value = [to.path]
})

// 菜单配置项
const menuItems = computed(() => {
  const items = [
    {
      key: '/',
      label: '首页',
      title: '首页'
    }
  ]

  // 如果是管理员，添加管理菜单
  if (loginUserStore.loginUser.userRole === 'admin') {
    items.push(
      {
        key: '/admin/user-manager',
        label: '用户管理',
        title: '用户管理'
      },
      {
        key: '/admin/app-manager',
        label: '应用管理',
        title: '应用管理'
      }
    )
  }

  return items
})

// 处理菜单点击
const handleMenuClick: MenuProps['onClick'] = e => {
  const key = e.key as string
  selectedKeys.value = [key]
  // 跳转到对应页面
  if (key.startsWith('/')) {
    router.push(key)
  }
}

const handleLogin = () => {
  router.push('/user/login')
}

const handleEditProfile = () => {
  router.push('/user/edit')
}

const handleLogout = () => {
  // 清除登录状态
  loginUserStore.setLoginUser({} as API.LoginUserVO)

  // 清除本地存储的token
  localStorage.removeItem('token')
  sessionStorage.removeItem('token')

  message.success('退出登录成功')

  // 跳转到登录页面
  router.push('/user/login')
}
</script>

<style scoped>
.header {
  background: #fff;
  padding: 0;
  height: 64px;
  line-height: 64px;
}

.header-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  width: 100%;
  padding: 0 24px;
}

.header-left {
  flex-shrink: 0;
}

.logo-section {
  display: flex;
  align-items: center;
  gap: 12px;
  text-decoration: none;
}

.logo {
  height: 40px;
  width: 40px;
}

.site-title {
  margin: 0;
  font-size: 18px;
  color: #1890ff;
  font-weight: 600;
}

.header-center {
  flex: 1;
  display: flex;
  justify-content: center;
  margin: 0 40px;
}

.nav-menu {
  border-bottom: none !important;
  background: transparent;
  line-height: 64px;
}

.header-right {
  flex-shrink: 0;
}

.user-login-status {
  display: flex;
  align-items: center;
}

.user-avatar-wrapper {
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 6px;
  transition: background-color 0.3s ease;
  display: flex;
  align-items: center;
}

.user-avatar-wrapper:hover {
  background-color: #f5f5f5;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-container {
    padding: 0 16px;
  }

  .header-center {
    margin: 0 20px;
  }

  .site-title {
    font-size: 16px;
  }
}

@media (max-width: 576px) {
  .site-title {
    display: none;
  }

  .header-center {
    margin: 0 10px;
  }
}
</style>
